# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=h2o
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.1
pkgrel=1
pkgdesc="The optimized HTTP/1, HTTP/2 server (mingw-w64)"
arch=('any')
url="https://github.com/h2o/h2o"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-wslay"
         "${MINGW_PACKAGE_PREFIX}-libuv")
options=('staticlibs' 'strip')

source=("https://github.com/h2o/h2o/archive/v$pkgver.tar.gz"
        "0001-update-yaml-to-0.1.6-for-security-fix.patch"
        "0002-basic-windows-support.patch"
        "0003-add-readdir_r-emulation-for-windows.patch"
        "0004-serverutil-hackery-for-windows.patch"
        "0005-mman-hackery-for-windows.patch"
        "0006-fix-gmtime_r-for-mingw-w64.patch"
        "0007-cloexec-hackery-for-windows.patch"
        "0008-ignore-O_CLOEXEC-in-file.c-for-windows.patch"
        "0009-use-gnu_printf-and-gnu_scanf-in-mingw-w64.patch"
        "0010-more-socket-work-for-windows-on-evloop.patch")

sha256sums=('8668a7c70cdb59eef4e67889569a45e0cf75b95eec133bd30435879cbdb77fba'
            'f79055e76acde4a8f7ab5518418f535ef92a4597cf34b6d70e9c652f9a31b05f'
            '92b93a4b47b21cc34dabe527cdab978f4c6224b76caf85287560dde6b412395c'
            'e0527b97a8ba668bacf077d2540ae0184339af36571f347b0be81131310ce702'
            '8098ac07cddb81fabeb14d701ea9ffe94d3d25b9b3c6c6a97bade926b75f6458'
            '3ac1ad8388f65c463d8e2a097ba4087d02c2712b99b13d558dd708f6ae467f92'
            '1c704e1e7465a895531dfa5b72ecaaa6da51088d0c3556455f93d96a1c614749'
            '6e61ee523048d2fe120397cf8c1619c0f0a920355152d9852c37637abefdacc5'
            '9f6f6728e5652fa0b0333c0b052f58310a9391603a0140ab2fb0e2bf3f46f269'
            'fa0028f61d83e89ced9ef1fcbfdac8b87f8e6e60014e3871910a7cb3df3c31b7'
            '866c9e8d3304f238c0cefae390d23453e9ab6931ec9a462cb116306116a43ee3')

prepare() {
  cd "$srcdir/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/0001-update-yaml-to-0.1.6-for-security-fix.patch
  patch -p1 -i "${srcdir}"/0002-basic-windows-support.patch
  patch -p1 -i "${srcdir}"/0003-add-readdir_r-emulation-for-windows.patch
  patch -p1 -i "${srcdir}"/0004-serverutil-hackery-for-windows.patch
  patch -p1 -i "${srcdir}"/0005-mman-hackery-for-windows.patch
  patch -p1 -i "${srcdir}"/0006-fix-gmtime_r-for-mingw-w64.patch
  patch -p1 -i "${srcdir}"/0007-cloexec-hackery-for-windows.patch
  patch -p1 -i "${srcdir}"/0008-ignore-O_CLOEXEC-in-file.c-for-windows.patch
  patch -p1 -i "${srcdir}"/0009-use-gnu_printf-and-gnu_scanf-in-mingw-w64.patch
  patch -p1 -i "${srcdir}"/0010-more-socket-work-for-windows-on-evloop.patch
}

build() {
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_C_FLAGS="-D__USE_MINGW_ANSI_STDIO=1" \
    -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX} \
    ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install
}
